define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lal/withdraw/index' + location.search,
                    add_url: 'lal/withdraw/add',
                    edit_url: 'lal/withdraw/edit',
                    del_url: 'lal/withdraw/del',
                    multi_url: 'lal/withdraw/multi',
                    import_url: 'lal/withdraw/import',
                    table: 'lal_member_withdraw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                Search:true,
                commonSearch:true,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'amount', title: __('Amount'), operate:false},
                        {field: 'card', title: __('Card'), operate:false},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'cate', title: __('Cate'), searchList: {"0":__('Cate 0'),"1":__('Cate 1')}, formatter: Table.api.formatter.normal},
                       // {field: 'shop_id', title: __('Shop_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                      //  {field: 'menber_id', title: __('Menber_id')},
                        
                     
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'code', title: __('Code'), operate: 'LIKE'},
                      //  {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                      //  {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'lal/withdraw/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'lal/withdraw/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'lal/withdraw/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            $(document).on("change", "select[name='row[type]']", function () {
                var type = $('select[name="row[type]"]').prop('value');
                if (type == "0") {
                    $('#c-shop_id_text').selectPageClear();
                    $('#c-shop_id_text').attr('placeholder','选择门店');
                    $("#c-shop_id_text").data("selectPageObject").option.data = "lal/member/getshoptypeList?type=1";
                }else{
                    $('#c-shop_id_text').selectPageClear();
                    $('#c-shop_id_text').attr('placeholder','选择服务商');
                    $("#c-shop_id_text").data("selectPageObject").option.data = "lal/member/getshoptypeList?type=2";
                }
            });

            $(document).on("change", "#c-shop_id_text", function () {
                let type = $("select[name='row[type]']").val();
                let shop_id = $("input[name='row[shop_id]']").val();
                if (shop_id) {
                    Fast.api.ajax({
                        url: 'lal/withdraw/getdetail',
                        data: {type: type,shop_id: shop_id}
                    }, function(data, ret) {
                        if (ret.code == 1) {
                            $(".valuebalance").text(ret.data.valuebalance);
                            $(".accountbalance").text(ret.data.accountbalance);
                            $(".withdraw_total").text(ret.data.withdraw_total);
                            $(".storedwithdrawtotal").text(ret.data.storedwithdrawtotal);
                            $(".accountwithdrawtotal").text(ret.data.accountwithdrawtotal);
                            $(".availablestoredwithdraw").text(ret.data.availablestoredwithdraw);
                            $(".availableaccountwithdraw").text(ret.data.availableaccountwithdraw);
                        }
                    });
                }
                
                return false;
            });

            $(document).on("click", ".btn-embossed", function () {
                var cate = $('input[name="row[cate]"]:checked').val();
                var amount = parseFloat($("#c-amount").val());
                var availablestoredwithdraw = parseFloat($(".availablestoredwithdraw").text());
                var availableaccountwithdraw = parseFloat($(".availableaccountwithdraw").text());

                if (cate === undefined) {
                    Layer.alert('请选择提现类型！！');
                    return false;
                }

                if (!amount || amount < 0 || amount == 0) {
                    Layer.alert('请填写正确的提现金额！！');
                    return false;
                }

                if (cate == 0 && amount > availablestoredwithdraw) {
                    Layer.alert('提现金额超出储值可提现金额');
                    return false;
                }

                if (cate == 1 && amount > availableaccountwithdraw) {
                    Layer.alert('提现金额超出账户可提现金额');
                    return false;
                }
                
            });
            Controller.api.bindevent();   
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
